<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-select v-model="periodNo" clearable placeholder="请选择周期号" size="mini"
					style="width: 150px; margin-right: 10px">
					<el-option v-for="item in periodNoList" :key="item.value" :label="item.label" :value="item.value"
						width="150px">
					</el-option>
				</el-select>
				<custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
				<custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
				<custom-button type="add" style="margin-right: 10px" @addClick="addClick"></custom-button>
				<!-- <custom-button
					type="add"
					title="参照新增"
					style="margin-right: 10px"
				></custom-button> -->
				<custom-button type="print" title="标签打印" @printClick="printClick"></custom-button>
			</div>
		</navigation-bar>
		<el-row>
			<el-col :span="6">
				<div style="width: 98%">
					<single-table :dataSource="leftDataSource" :tableColumns="leftTableColumns"
						@singleHandleSelectionChange="singleHandleSelectionChange"></single-table>
				</div>
			</el-col>
			<el-col :span="18">

				<table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
					:pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow" @viewRow="viewRow"
					@deleteRow="deleteRow" selection @handleSelectionChange="handleSelectionChange"></table-index>
			</el-col>
		</el-row>
		<div v-if="dialogShow">
			<custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
				<form-index :formConfig="formConfig" @submit="submit" :type="type" @formInputChange="formInputChange"
					ref="form">
					<!--  <div>这里还可以插入其它内容进行拓展</div> -->
				</form-index>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
import { iceStorageBoxInformationForm } from "@/assets/form/managementCenter/sampleManagement/iceStorageBoxInformationForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import singleTable from '@/components/customComponents/customeTable/singleTable'
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import { AjaxCommon, printAddress } from "@/libs/apiCommon"
import { CardId } from "@/libs/cardId"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"

export default {
	components: {
		navigationBar,
		customButton,
		tableIndex,
		customDialog,
		formIndex,
		singleTable,
	},
	data() {
		return {
			selectDeviceList: [],
			periodNo: "",
			periodNoList: [
				{
					label: "第一周期",
					value: "1",
				},
				{
					label: "第二周期",
					value: "2",
				},
				{
					label: "第三周期",
					value: "3",
				},
				{
					label: "第四周期",
					value: "4",
				},
			],
			leftDataSource: [],
			leftTableColumns: [
				{
					prop: 'typeName',
					label: '试验样品名称',
					twoColumns: ['typeName', 'testIndex'],
				},
			],
			// table 相关参数  begin
			dataSource: [],
			tableColumns: [
				{
					prop: "useType",
					label: "存放类型",
					width: 140,
					dictName: "tube_type",
				},

				{
					prop: "temperature",
					label: "存放温度",
					width: 120,
				},
				{
					prop: "rowsNum",
					label: "行数",
					width: 120,
				},
				{
					prop: "columnNum",
					label: "列数",
					width: 120,
				},
				{
					prop: "assignSamplePoint",
					label: "指定采样点",
				},
				{
					prop: "remarks",
					label: "备注",
				},
			],
			btnList: ["edit", "view", "delete"],
			pageSize: 20,
			pageCount: 100,
			pageNo: 1,
			// table 相关参数  end

			// 弹窗是否显示
			dialogShow: false,
			// 弹窗标题 (新增修改不同)
			dialogTitle: "",

			// form的配置信息
			formConfig: [],
			type: "add",
		}
	},
	methods: {
		// 获取采集点
		getTimePointSelect(sampleTypeId) {
			console.log(sampleTypeId);
			let self = this
			AjaxCommon("/ccdc/businessSelect/timePointSelect", "post", { 'sampleTypeId': sampleTypeId }).then(
				(res) => {
					if (res.data.code == 200) {
						if (res.data.result.length > 0) {
							const sampleCollectionPointData = res.data.result.filter(
								(t) => t.value
							)
							const fieldList = iceStorageBoxInformationForm(
								sampleCollectionPointData
							)
							self.formConfig = fieldList
						}
					}
				}
			)
		},
		handleSelectionChange(d) {
			this.selectDeviceList = d
		},
		printClick() {
			let urlAdd = `${printAddress}/ccdc/tmrreport/pdf/show?1=1&_u=file:shiguanhebqV1.tmrreport.xml&_i=1&_r=1
`
			if (this.selectDeviceList.length > 0) {
				let ids = this.selectDeviceList
					.map((t) => {
						return t.id
					})
					.join(",")
				if (this.selectDeviceList.length < 2) {
					urlAdd += "&id=" + ids
				} else {
					urlAdd += "&ids=" + ids
				}
			}
			let projectTestperiod =
				window.sessionStorage.getItem("projectTestperiod")
			if (projectTestperiod) {
				projectTestperiod = JSON.parse(projectTestperiod)
				urlAdd += `&pmId=${projectTestperiod.projectId}&expId=${projectTestperiod.testId
					}&periodNo=${projectTestperiod.periodId ? projectTestperiod.periodId : 1
					}`
			}

			console.log(urlAdd)
			window.open(urlAdd)
		},
		formInputChange(e) {
			if (e.filedName == "idCard") {
				const data = CardId.getInfoByCardId(e.value)
				if (data) {
					let newData = e.allData
					newData["birthday"] = data.birthday
					newData["sex"] = data.sex
					this.$refs.form.changeFormValue(newData)
				}
			}
		},
		submit(d) {
			let self = this
			console.log(d)
			if (d.id) {
				AjaxCommon("/ccdc/pm/sample/sampleLceStoreBox/update", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			} else {
				delete d.id
				d.sampleType = this.selectData.length > 0 ? this.selectData[0].type : ""
				d.testIndex = this.selectData.length > 0 ? this.selectData[0].testIndex : ""
				d.collectCondition = this.selectData.length > 0 ? this.selectData[0].collectionCondition : ""
				d.sampleTypeId = this.selectData.length > 0 ? this.selectData[0].id : ""
				AjaxCommon("/ccdc/pm/sample/sampleLceStoreBox/add", "post", d)
					.then((res) => {
						if (res.data.code == 200) {
							PopupCommon.notify("success", "操作成功")
							self.initPage()
							self.dialogClose()
						} else {
							PopupCommon.notify("error", res.data.message)
						}
					})
					.catch((err) => {
						PopupCommon.notify("error", err.message)
					})
			}
		},
		dialogClose() {
			this.dialogShow = false
		},
		searchClick() {
			this.pageNo = 1
			this.initPage()
		},
		resetClick() {
			this.name = ""
		},
		addClick() {
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = ""
					}
				})
			})
			this.formConfig = formConfigItem
			this.type = "add"
			this.dialogShow = true
			this.dialogTitle =
				"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		// 分页点击事件
		pageChange(p) {
			this.pageNo = p
			this.initPage()
		},
		editRow(d) {
			this.type = "edit"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = d.data[t[res]]
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		viewRow(d) {
			this.type = "look"
			let formConfigItem = this.formConfig
			formConfigItem.map((t) => {
				Object.keys(t).map((res) => {
					if (res == "fieldName") {
						t.filedValue = d.data[t[res]]
					}
				})
			})
			this.formConfig = formConfigItem
			this.dialogShow = true
			this.dialogTitle =
				"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
		},
		deleteRow(d) {
			const id = d.data.id
			let self = this
			AjaxCommon(
				"/ccdc/pm/sample/sampleLceStoreBox/delete?id=" + id,
				"delete",
				{ optReason: d.optReason }
			).then((res) => {
				console.log(res, "res...............")
				if (res.data.code == 200) {
					PopupCommon.notify("success", "删除成功")
					self.initPage()
					self.dialogClose()
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
		// 左侧选中事件
		singleHandleSelectionChange(d) {
			this.selectData = d
			this.leftSelectIds = d ? d.map((t) => t.id) : []
			this.initPage()
			if (d) {
				this.getTimePointSelect(d[0].id)
			}
		},
		// 初始化事件
		initPage() {
			let self = this
			AjaxCommon("/ccdc/pm/sample/sampleLceStoreBox/listByPage", "post", {
				sampleTypeIdList: this.leftSelectIds,
				periodNo: this.periodNo,
				pageNo: this.pageNo,
				pageSize: this.pageSize,
			}).then((res) => {
				if (res.data.code == 200) {
					self.dataSource = res.data.result.records
					self.pageCount = res.data.result.total
				} else {
					PopupCommon.notify("error", res.data.message)
				}
			})
		},
		initLeftData() {
			AjaxCommon('/ccdc/cfg/exp/root/expSample/listByPage', 'post', {}).then(
				(res) => {
					if (res.data.code == 200) {
						this.leftDataSource = res.data.result.records
					} else {
						this.leftDataSource = []
						PopupCommon.notify('error', res.data.message)
					}
				}
			)
		},
	},
	mounted() {
		this.initPage()
		this.initLeftData()
	},
}
</script>

<style></style>
